package com.itheima.web.manager;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.TestSoul;
import com.itheima.domain.db.TestSoulQuestion;
import com.itheima.domain.db.TestSoulQuestionsOption;
import com.itheima.service.db.TestSoulService;
import com.itheima.util.ResultInfo;
import com.itheima.vo.PageBeanVo;
import com.itheima.web.exception.CustomException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class TestSoulManager {

    @DubboReference
    private TestSoulService testSoulService;

    //题目列表
    public ResultInfo findTestSoulList(Integer pageNum, Integer pageSize, String name) {
        //1.rpc调用service
        PageBeanVo pageBeanVo = testSoulService.testSoulList(pageNum, pageSize, name);
        List<TestSoul> testSoulList = (List<TestSoul>) pageBeanVo.getItems();
        Page<TestSoul> page = new Page<>(pageNum, pageSize);
        page.setRecords(testSoulList);
        page.setTotal(pageBeanVo.getCounts());
        //返回
        return ResultInfo.success(page);
    }

    //问题列表
    public ResultInfo findQuestionList(Integer pageNum, Integer pageSize, String name, String timuId) {
        //1.rpc调用service
        Long id = Long.valueOf(timuId);
        PageBeanVo pageBeanVo = testSoulService.QuestionList(pageNum, pageSize, name, id);
        List<TestSoulQuestion> testSoulQuestionList = (List<TestSoulQuestion>) pageBeanVo.getItems();
        Page<TestSoulQuestion> page = new Page<>(pageNum, pageSize);
        page.setRecords(testSoulQuestionList);
        page.setTotal(pageBeanVo.getCounts());
        return ResultInfo.success(page);
    }

    //答案列表
    public ResultInfo findAnswerList(Integer pageNum, Integer pageSize, String name, String wentiId) {
        //1.rpc调用service
        Long id = Long.valueOf(wentiId);
        PageBeanVo pageBeanVo = testSoulService.answerList(pageNum, pageSize, name, id);
        List<TestSoulQuestionsOption> list = (List<TestSoulQuestionsOption>) pageBeanVo.getItems();
        Page<TestSoulQuestionsOption> page = new Page<>(pageNum, pageSize);
        page.setRecords(list);
        page.setTotal(pageBeanVo.getCounts());
        return ResultInfo.success(page);
    }

    //新增答案
    public void addAnswer(TestSoulQuestionsOption option) {
        //1.rpc调用service
        testSoulService.addAnswer(option);
    }

    //修改答案
    public void editAnswer(TestSoulQuestionsOption option) {
        //1.rpc调用service
        option.setUpdated(new Date());
        testSoulService.editAnswer(option);
    }

    //删除答案
    public void deleteAnswer(Long answerId) {
        //1.rpc调用service
        testSoulService.deleteAnswer(answerId);
    }

    //根据id删除问题
    public ResultInfo deleteQuestion(Long questionId) {
        //1.先查询问题下是否有答案
        Integer count = testSoulService.findAnswerByQuestionId(questionId);
        //2.如果有，抛异常
        if (count!=0){
            throw new CustomException("该问题下有答案，不能删除");
        }else {//3.没有，执行删除
            testSoulService.deleteQuestion(questionId);
        }
        return ResultInfo.success(null);
    }

    //新增问题
    public void addQuestion(TestSoulQuestion question) {
        //调用service
        testSoulService.addQuestion(question);
    }

    //新增题目
    public void addTest(TestSoul testSoul) {
        //调用service
        testSoulService.addTest(testSoul);
    }

    //删除题目
    public void deleteTest(Long testId) {
        //1.判断题目下有没有问题
        Integer count = testSoulService.findQuestionByTestId(testId);
        //2.有，抛异常
        if (count!=0){
            throw new CustomException("题目下有问题，不能删除");
        }
        //3.没有，执行删除
        testSoulService.deleteTest(testId);
    }
}
